package com.pbxadmin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.google.gson.Gson;
import com.pbxadmin.cfg.WSConstants;
import com.pbxadmin.form.SipPeersBatch;
import com.pbxadmin.pojo.SipPeer;
import com.pbxadmin.pojo.SipPeers;
import com.pbxadmin.service.PbxWebService;

@Controller
@RequestMapping("/sippeer")
public class SipPeerController {
	
	@Autowired
	private PbxWebService pbxWebService;
	@Autowired
	private Gson gson;
	
	private Logger log = LoggerFactory.getLogger(SipPeerController.class);
	/**
	 * 列表
	 */ 
	@RequestMapping("/list")
	public ModelAndView list(){
		String returnCode = pbxWebService.invokePbx("list_sip_peers", null);
		if(returnCode.isEmpty()){
			return new ModelAndView("/error/pbxerror","message","pbxservice 发生错误，请稍候再试。");
		}
		
		List<SipPeers> sipPeers = gson.fromJson(returnCode, WSConstants.listSipPeers);
		ModelAndView mav = new ModelAndView("/sippeer/list");
		mav.addObject("sipPeers", sipPeers);
		return mav;
	}
	/**
	 * 详情
	 */
	@RequestMapping("/detail/{userName}")
	public ModelAndView detail(HttpServletRequest req,@PathVariable("userName") String userName){
		if(userName == null || userName.isEmpty()){
			return new ModelAndView("/error/parameters","message","/sippeer/detail/{userName} , userName is null");
		}
		
		ModelAndView mav = new ModelAndView("/sippeer/detail");
		String returnCode = pbxWebService.invokePbx("list_sip_peer", new Object[]{userName});
		SipPeer sipPeer = gson.fromJson(returnCode,SipPeer.class);
		mav.addObject("sipPeer", sipPeer);
		return mav;
	}
	/**
	 * 修改初始化
	 */
	@RequestMapping("/modify/{userName}/init")
	public ModelAndView modifyInit(HttpServletRequest req,@PathVariable("userName") String userName){
		if(userName == null || userName.isEmpty()){
			return new ModelAndView("/error/parameters","message","/sippeer/detail/{userName} , userName is null");
		}
		String result = req.getParameter("result");
		if(result != null){
			result = new String(Base64.decodeBase64(result));
		}else{
			result = "";
		}
		ModelAndView mav = new ModelAndView("/sippeer/modify");
		String returnCode = pbxWebService.invokePbx("list_sip_peer", new Object[]{userName});
		SipPeer sipPeer = gson.fromJson(returnCode,SipPeer.class);
		mav.addObject("sipPeer", sipPeer);
		mav.addObject("userName", userName);
		mav.addObject("result", result);
		return mav;
	}
	/**
	 * 修改
	 */
	@RequestMapping("/modify/{userName}")
	public ModelAndView modify(HttpServletRequest req,@PathVariable("userName") String userName,@ModelAttribute SipPeer sipPeer){
		if(userName == null || userName.isEmpty()){
			return new ModelAndView("/error/parameters","message","/sippeer/detail/{userName} , userName is null");
		}
		
		String gsonStr = gson.toJson(sipPeer);
		//String returnCode = WSConstants.RTN_SUCCESS;
		String returnCode = pbxWebService.invokePbx("mod_sip_peer", new Object[]{userName,gsonStr});
		String result = "";
		if(WSConstants.RTN_SUCCESS.equals(returnCode)){
			result = "修改分机["+userName+"]成功";
		}else{
			result = "修改分机["+userName+"]失败";
		}
		result = Base64.encodeBase64URLSafeString(result.getBytes());
		return new ModelAndView(new RedirectView("/pbxadmin/sippeer/modify/"+userName+"/init"),"result",result);
	}
	
	/**
	 * 添加初始化
	 */
	@RequestMapping("/add/init")
	public ModelAndView addInit(HttpServletRequest req){
		ModelAndView mav = new ModelAndView("/sippeer/add");
		String result = req.getParameter("result");
		if(result != null){
			result = new String(Base64.decodeBase64(result));
		}else{
			result = "";
		}
		mav.addObject("result", result);
		return mav;
	}
	
	/**
	 * 添加
	 */
	@RequestMapping("/add/batch")
	public ModelAndView addBatch(@ModelAttribute SipPeersBatch sipPeersBatch){
		//add_sip_peers ($min, $max, $default_pwd, $call_group, $pickup_group, $call_access)
		String min = sipPeersBatch.getMinSipNum();
		String max = sipPeersBatch.getMaxSipNum();
		String defaultPwd = sipPeersBatch.getDefaultPwd();
		String callGroup = sipPeersBatch.getCallGroup();
		String pickupGroup = sipPeersBatch.getPickupGroup();
		String callAccess = sipPeersBatch.getCallAccess();
		
		String returnCode = pbxWebService.invokePbx("add_sip_peers", new Object[]{min,max,defaultPwd,callGroup,pickupGroup,callAccess});
		
		String result = "";
		if(WSConstants.RTN_SUCCESS.equals(returnCode)){
			result = "批量添加分机"+sipPeersBatch.getMinSipNum()+"--"+sipPeersBatch.getMaxSipNum()+"成功";
		}else{
			result = "批量添加分机"+sipPeersBatch.getMinSipNum()+"--"+sipPeersBatch.getMaxSipNum()+"失败";
		}
		result = Base64.encodeBase64URLSafeString(result.getBytes());
		
		return new ModelAndView(new RedirectView("init"),"result",result);
	}
	
	

	public PbxWebService getPbxWebService() {
		return pbxWebService;
	}

	public void setPbxWebService(PbxWebService pbxWebService) {
		this.pbxWebService = pbxWebService;
	}

	public Gson getGson() {
		return gson;
	}

	public void setGson(Gson gson) {
		this.gson = gson;
	}
}
